package com.dream.fragment;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dream.classcontrol.MainActivity;
import com.dream.classcontrol.R;
import com.dream.config.DevUIIDConfig;
import com.dream.sdkmanager.SDKManager;

import java.util.Timer;
import java.util.TimerTask;


public class KongtiaoFragment extends android.app.Fragment implements View.OnClickListener {

    private TextView kongtiaoValue;
    private ImageView kongtiaoPower;
    private TextView kongtiao26di;
    private TextView kongtiao26gao;
    private TextView kongtiao27di;
    private TextView kongtiao27gao;
    private TextView kongtiao28di;
    private TextView kongtiao28gao;
    private TextView kongtiao29di;
    private TextView kongtiao29gao;
    private TextView kongtiaoLen;
    private TextView kongtiaoRe;
    private TextView[] textViews;
    private int powerState=0;

    private MainActivity mainActivity;
    private SDKManager sdkManager;
    private Timer timerWendu;
    private static final int ROOM_WENDU=0;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case ROOM_WENDU:
                    kongtiaoValue.setText(mainActivity.getRoomWendu());
                    break;
            }
        }
    };

    public KongtiaoFragment() {

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mainActivity= (MainActivity) activity;
        sdkManager=mainActivity.getSdkManager();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_kongtiao, container, false);
        initView(view);
        setOnClick();
        return view;
    }

    public void initView(View view){
        kongtiaoValue = (TextView) view.findViewById(R.id.kongtiao_value);
        kongtiaoPower = (ImageView) view.findViewById(R.id.kongtiao_power);
        kongtiao26di = (TextView) view.findViewById(R.id.kongtiao_26di);
        kongtiao26gao = (TextView) view.findViewById(R.id.kongtiao_26gao);
        kongtiao27di = (TextView) view.findViewById(R.id.kongtiao_27di);
        kongtiao27gao = (TextView) view.findViewById(R.id.kongtiao_27gao);
        kongtiao28di = (TextView) view.findViewById(R.id.kongtiao_28di);
        kongtiao28gao = (TextView) view.findViewById(R.id.kongtiao_28gao);
        kongtiao29di = (TextView) view.findViewById(R.id.kongtiao_29di);
        kongtiao29gao = (TextView) view.findViewById(R.id.kongtiao_29gao);
        kongtiaoLen = (TextView) view.findViewById(R.id.kongtiao_len);
        kongtiaoRe = (TextView) view.findViewById(R.id.kongtiao_re);
        textViews=new TextView[]{kongtiao26di,kongtiao26gao,kongtiao27di,kongtiao27gao,kongtiao28di,kongtiao28gao,kongtiao29di,kongtiao29gao};
        timerWendu=new Timer();
        timerWendu.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(ROOM_WENDU);
            }
        },0,1000);
    }

    public void setOnClick(){
        kongtiao26di.setOnClickListener(this);
        kongtiao26gao.setOnClickListener(this);
        kongtiao27di.setOnClickListener(this);
        kongtiao27gao.setOnClickListener(this);
        kongtiao28di.setOnClickListener(this);
        kongtiao28gao.setOnClickListener(this);
        kongtiao29di.setOnClickListener(this);
        kongtiao29gao.setOnClickListener(this);
        kongtiaoLen.setOnClickListener(this);
        kongtiaoRe.setOnClickListener(this);
        kongtiaoPower.setOnClickListener(this);
    }

    public void sendKongTiaoKey(int key){
        if (sdkManager.getLandingSate()){
            sdkManager.senKey(DevUIIDConfig.kongtiao,key);
        }else {
            Toast.makeText(getActivity(),"请登录后重试",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.kongtiao_26di:
                setTextViewBg(0);
                sendKongTiaoKey(1);
                break;
            case R.id.kongtiao_26gao:
                setTextViewBg(1);
                sendKongTiaoKey(2);
                break;
            case R.id.kongtiao_27di:
                setTextViewBg(2);
                sendKongTiaoKey(3);
                break;
            case R.id.kongtiao_27gao:
                setTextViewBg(3);
                sendKongTiaoKey(1);
                break;
            case R.id.kongtiao_28di:
                setTextViewBg(4);
                sendKongTiaoKey(2);
                break;
            case R.id.kongtiao_28gao:
                setTextViewBg(5);
                sendKongTiaoKey(3);
                break;
            case R.id.kongtiao_29di:
                setTextViewBg(6);
                sendKongTiaoKey(1);
                break;
            case R.id.kongtiao_29gao:
                setTextViewBg(7);
                sendKongTiaoKey(2);
                break;
            case R.id.kongtiao_len:
                kongtiaoLen.setTextColor(Color.parseColor("#2F97FC"));
                kongtiaoRe.setTextColor(Color.parseColor("#A5A1A1"));
                sendKongTiaoKey(3);
                break;
            case R.id.kongtiao_re:
                kongtiaoLen.setTextColor(Color.parseColor("#A5A1A1"));
                kongtiaoRe.setTextColor(Color.parseColor("#2F97FC"));
                sendKongTiaoKey(1);
                break;
            case R.id.kongtiao_power:
                if (powerState==0){
                    kongtiaoPower.setImageResource(R.drawable.bg_kongtiao_on);
                    powerState++;
                    //开启
                    sendKongTiaoKey(2);
                }else {
                    kongtiaoPower.setImageResource(R.drawable.bg_kongtiao_off);
                    powerState--;
                    //关闭
                    sendKongTiaoKey(3);
                }
                break;
        }
    }

    //true 开 false 关
    public boolean setTextViewBg(int index){
        boolean state=false;
        for (int i = 0; i < textViews.length; i++) {
            if (index==i){
                textViews[i].setBackgroundResource(R.drawable.bg_blue);
                textViews[i].setTextColor(Color.parseColor("#FFFFFF"));
                state=true;
            }else {
                textViews[i].setBackgroundResource(R.drawable.bg_wihte);
                textViews[i].setTextColor(Color.parseColor("#2F97FC"));
                state=false;
            }
        }
        return state;
    }
}
